renv::status()
library(readr)
# Chunk 1
#install.packages('renv')
#renv::init()
# Chunk 2
library(readr)
d <- read_csv("./data/luck_questions.csv", col_names=TRUE)
# Chunk 3
d$userid <- as.character(d$userid)
head(d$userid)
# Chunk 4
d$ordercapture <- d$ql2ordercapture; head(d$ordercapture)
#remove special characters; e.g., '[', ',', and ']'
## leave space separators
d$ordercapture <- gsub('[^[:alnum:] ]', '', d$ordercapture)
head(d$ordercapture)
# Chunk 5
library(tidyr)
install.packages("tidyr")
# Chunk 1
#install.packages('renv')
#renv::init()
# Chunk 2
library(readr)
d <- read_csv("./data/luck_questions.csv", col_names=TRUE)
# Chunk 3
d$userid <- as.character(d$userid)
head(d$userid)
# Chunk 4
d$ordercapture <- d$ql2ordercapture; head(d$ordercapture)
#remove special characters; e.g., '[', ',', and ']'
## leave space separators
d$ordercapture <- gsub('[^[:alnum:] ]', '', d$ordercapture)
head(d$ordercapture)
# Chunk 5
library(tidyr)
#create order labels (for columns)
orders <- paste0("q", seq(1,7,1)); orders
#split ordercapture vector into 7 columns
d <- separate(data=d, col=ordercapture, into=orders,
sep=' ', remove=TRUE, convert=FALSE)
# Chunk 6
library(reshape2)
install.packages("reshape2")
install.packages("ggplot2")
install.packages("RColorBrewer")
install.packages("ggthemes")
# Chunk 1
#install.packages('renv')
#renv::init()
# Chunk 2
library(readr)
d <- read_csv("./data/luck_questions.csv", col_names=TRUE)
# Chunk 3
d$userid <- as.character(d$userid)
head(d$userid)
# Chunk 4
d$ordercapture <- d$ql2ordercapture; head(d$ordercapture)
#remove special characters; e.g., '[', ',', and ']'
## leave space separators
d$ordercapture <- gsub('[^[:alnum:] ]', '', d$ordercapture)
head(d$ordercapture)
# Chunk 5
library(tidyr)
#create order labels (for columns)
orders <- paste0("q", seq(1,7,1)); orders
#split ordercapture vector into 7 columns
d <- separate(data=d, col=ordercapture, into=orders,
sep=' ', remove=TRUE, convert=FALSE)
# Chunk 6
library(reshape2)
#Column names of likert responses
r <- paste0("ql2r", seq(1,7,1)); r
#melt data
dr <- melt(d[,c("userid",r)], id='userid')
str(dr$value)
#vector of factor format
dr$factor <- as.factor(dr$value)
str(dr$factor)
levels(dr$factor)
#vector of luck type description
dr$luck_type <- dr$variable
levels(dr$luck_type) <- c("The family or society that you are born into", #ql2r1
"Unforeseen events (e.g. financial crises or booms, natural disasters, good harvests)", #ql2r2
"Access to education (e.g. quality of public schools in district)", #ql2r3
"Unexpected windfalls (e.g. winning the lottery, inheritance)", #ql2r4
"Your social and professional network", #ql2r5
"Fortuitous encounters and coincidences (e.g. right place at the right time)", #ql2r6
"Genetic makeup or natural ability" #ql2r7
)
head(dr); tail(dr)
# Chunk 7
library(ggplot2)
library(RColorBrewer)
library(ggthemes)
# Chunk 8
library(dplyr)
responses <- as.factor(c(1,2,3,4,5))
df <- data.frame(luck_type=dr$luck_type, importance=dr$factor)
head(df)
df %>%
group_by(luck_type, importance) %>%
count() %>%
ungroup() %>%
group_by(luck_type) %>%
mutate(percent = 100*n / sum(n)) %>%
mutate(percent = round(percent, 0)) %>%
mutate(bar_text = paste0(percent, "%")) %>%
ungroup() -> df_for_plotting
head(df_for_plotting)
nna <- which(is.na(df_for_plotting$importance)==F)
df_for_plotting <- df_for_plotting[nna, ]
head(df_for_plotting)
df_for_plotting %>%
filter(importance == responses[5]) %>%
arrange(percent) %>%
pull(luck_type) -> order_y
head(order_y)
df_for_plotting %>%
mutate(luck_type = factor(luck_type, levels = order_y)) %>%
mutate(importance = factor(importance, levels = responses[5:1])) -> df_ordered
head(df_ordered)
# Chunk 9
my_font <- "Roboto Condensed"
#my_font <- "Sans"
library(showtext)
install.packages("showtext")
install.packages("ggtext")
# Chunk 1
#install.packages('renv')
#renv::init()
# Chunk 2
library(readr)
d <- read_csv("./data/luck_questions.csv", col_names=TRUE)
# Chunk 3
d$userid <- as.character(d$userid)
head(d$userid)
# Chunk 4
d$ordercapture <- d$ql2ordercapture; head(d$ordercapture)
#remove special characters; e.g., '[', ',', and ']'
## leave space separators
d$ordercapture <- gsub('[^[:alnum:] ]', '', d$ordercapture)
head(d$ordercapture)
# Chunk 5
library(tidyr)
#create order labels (for columns)
orders <- paste0("q", seq(1,7,1)); orders
#split ordercapture vector into 7 columns
d <- separate(data=d, col=ordercapture, into=orders,
sep=' ', remove=TRUE, convert=FALSE)
# Chunk 6
library(reshape2)
#Column names of likert responses
r <- paste0("ql2r", seq(1,7,1)); r
#melt data
dr <- melt(d[,c("userid",r)], id='userid')
str(dr$value)
#vector of factor format
dr$factor <- as.factor(dr$value)
str(dr$factor)
levels(dr$factor)
#vector of luck type description
dr$luck_type <- dr$variable
levels(dr$luck_type) <- c("The family or society that you are born into", #ql2r1
"Unforeseen events (e.g. financial crises or booms, natural disasters, good harvests)", #ql2r2
"Access to education (e.g. quality of public schools in district)", #ql2r3
"Unexpected windfalls (e.g. winning the lottery, inheritance)", #ql2r4
"Your social and professional network", #ql2r5
"Fortuitous encounters and coincidences (e.g. right place at the right time)", #ql2r6
"Genetic makeup or natural ability" #ql2r7
)
head(dr); tail(dr)
# Chunk 7
library(ggplot2)
library(RColorBrewer)
library(ggthemes)
# Chunk 8
library(dplyr)
responses <- as.factor(c(1,2,3,4,5))
df <- data.frame(luck_type=dr$luck_type, importance=dr$factor)
head(df)
df %>%
group_by(luck_type, importance) %>%
count() %>%
ungroup() %>%
group_by(luck_type) %>%
mutate(percent = 100*n / sum(n)) %>%
mutate(percent = round(percent, 0)) %>%
mutate(bar_text = paste0(percent, "%")) %>%
ungroup() -> df_for_plotting
head(df_for_plotting)
nna <- which(is.na(df_for_plotting$importance)==F)
df_for_plotting <- df_for_plotting[nna, ]
head(df_for_plotting)
df_for_plotting %>%
filter(importance == responses[5]) %>%
arrange(percent) %>%
pull(luck_type) -> order_y
head(order_y)
df_for_plotting %>%
mutate(luck_type = factor(luck_type, levels = order_y)) %>%
mutate(importance = factor(importance, levels = responses[5:1])) -> df_ordered
head(df_ordered)
# Chunk 9
my_font <- "Roboto Condensed"
#my_font <- "Sans"
library(showtext)
font_add_google(name = my_font, family = my_font)
showtext_auto()
library(ggtext) # Ref: https://cran.r-project.org/web/packages/ggtext/index.html
# Prepare title + subtitle + caption:
p_title <- "Important Luck Factors"
p_subtitle = "Circumstances beyond a person’s control—or, their luck—can impact their life earnings. Please indicate how important you think the following factors are for determining people’s life earnings."
p_caption <- "1= not important at all ... 5 = extremely important."
#theme_set(theme_minimal())
# Chunk 10
gg1 <- ggplot(data=df_ordered, aes(y=luck_type, x=percent, fill=importance)) +
geom_col(width=0.8, position="fill") +
theme(plot.margin = unit(rep(0.7, 4), "cm")) +
scale_fill_manual(values = c(`1` = "#e36c33",
`2` = "#edad88",
`3` = "#c7cdd1",
`4` = "#829cb2",
`5` = "#3e6487"),
breaks = c("1","2","3","4","5")) +
scale_x_continuous(expand = c(0, 0),
labels = paste0(seq(0, 100, 25), "%")) +
scale_y_discrete(expand = c(0, 0)) +
labs(fill = p_caption) +
theme(text = element_text(family = my_font)) +
theme(legend.position = "top",
#legend.justification='left',
legend.box = "horizontal",
legend.title = element_text(size = 16,
family = my_font,
color = "grey10"),
legend.text = element_text(size = 16,
family = my_font,
color = "grey10"),
legend.key.height = unit(0.35, "cm"),
legend.key.width = unit(0.15*3, "cm"),
legend.box.margin = margin(0,1,0,0,"cm")
) +
theme(axis.title = element_blank()) +
theme(panel.grid.minor = element_blank()) +
theme(panel.grid.major.x = element_line(color = "grey70",
size = 0.8)) +
theme(axis.text = element_text(color = "grey30",
size = 18,
family = my_font)) #+
#theme(plot.title = element_markdown(size = 16, face = "bold")) +
#theme(plot.caption = element_text(size = 10.5,
#                                  color = "grey40",
#                                  vjust = -1.5,
#                                  hjust = 0)) +
#theme(plot.subtitle = element_text(size = 11.5,
#                                   color = "grey10")) +
#theme(plot.title.position = "plot") +
#theme(plot.caption.position = "plot")
# For displaying percent of "5":
df_ordered %>%
filter(importance == "5") -> df_for_text1
# Ad text layers:
gg1 <- gg1 +
geom_text(data = df_for_text1 %>% filter(percent > 2),
aes(y = luck_type, x = 0.98, label = bar_text),
size = 6.5, color = "white", family = my_font, hjust = 1.2)
gg1
ggsave(filename = "./output/lucklikert.png", plot=gg1,
width=6.5, height=2.5, units="in", dpi=300)
